package org.qiyu.live.api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.juli.logging.Log;
import org.qiyu.live.user.dto.UserDTO;
import org.qiyu.live.user.interfaces.IUserPhoneRPC;
import org.qiyu.live.user.interfaces.IUserRpc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    private final Logger LOOGER = Logger.getLogger(UserController.class.getName());

    @DubboReference
    private IUserRpc userRpc;
    @DubboReference
    private IUserPhoneRPC userPhoneRPC;

    @GetMapping(value = "/getUserInfo")
    public UserDTO getUserInfo(Long userId) {
        UserDTO userDTO = userRpc.getByUserId(userId);
        if (userDTO != null) {
            LOOGER.info(userDTO.toString());
        }
        return userDTO;
    }

    @GetMapping(value = "/insertOne")
    public boolean insertOne(Long userId, String nickName) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setNickName(nickName);
        return userRpc.insertOne(userDTO);
    }

    @GetMapping(value = "/insertUserPhone")
    public boolean insertUserPhone(String phone) {
        return userPhoneRPC.insertUserPhone(phone);
    }

    @GetMapping(value = "/updateUserInfo")
    public boolean updateUserInfo(Long userId, String nickName) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setNickName(nickName);
        return userRpc.updateUserInfo(userDTO);
    }

    @GetMapping(value = "/batchQueryUserInfo")
    public Map<Long, UserDTO> batchQueryUserInfo(String userIdStr) {
        List<Long> userIdList = Arrays.stream(userIdStr.split(",")).map(Long::valueOf).collect(Collectors.toList());
        return userRpc.batchQueryUserInfo(userIdList);
    }

    @GetMapping(value = "/do-test")
    public String doTest() {
        userRpc.test();
        return "success";
    }
}
